package com.pet.behavior.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pet.behavior.mapper.ApUnlikesBehaviorMapper;
import com.pet.behavior.service.ApBehaviorEntryService;
import com.pet.behavior.service.ApUnlikesBehaviorService;
import com.pet.common.exception.LeadNewsException;
import com.pet.model.behavior.dtos.UnLikesBehaviorDto;
import com.pet.model.behavior.pojos.ApBehaviorEntry;
import com.pet.model.behavior.pojos.ApUnlikesBehavior;
import com.pet.model.common.dtos.ResponseResult;
import com.pet.model.common.enums.AppHttpCodeEnum;
import com.pet.model.user.pojos.ApUser;
import com.pet.utils.threadlocal.ThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * APP不喜欢行为表 服务实现类
 * </p>
 */
@Service
@Transactional
public class ApUnlikesBehaviorServiceImpl extends ServiceImpl<ApUnlikesBehaviorMapper, ApUnlikesBehavior> implements ApUnlikesBehaviorService {

    @Autowired
    private ApBehaviorEntryService apBehaviorEntryService;

    @Override
    public ResponseResult unLike(UnLikesBehaviorDto dto) {
        //1.取出登录用户信息
        ApUser apUser = (ApUser) ThreadLocalUtils.get();
        Integer userId = null;
        if(apUser!=null){
            userId=apUser.getId();
        }
        //2.取出设备信息
        Integer equipmentId = dto.getEquipmentId();
        //3.查询行为实体
        ApBehaviorEntry behaviorEntry = apBehaviorEntryService.findByUserIdOrEquipmentId(userId, equipmentId);
        if(behaviorEntry==null){
            throw new LeadNewsException(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        try {
            //4.查询阅读记录是否存在
            QueryWrapper<ApUnlikesBehavior> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("entry_id",behaviorEntry.getId());
            queryWrapper.eq("article_id",dto.getArticleId());
            ApUnlikesBehavior unlikesBehavior = getOne(queryWrapper);
            //4.1 不存在，添加不喜欢记录
            if(unlikesBehavior==null){
                unlikesBehavior = new ApUnlikesBehavior();
                unlikesBehavior.setEntryId(behaviorEntry.getId());
                unlikesBehavior.setArticleId(dto.getArticleId());
                unlikesBehavior.setType(dto.getType());
                unlikesBehavior.setCreatedTime(new Date());
                save(unlikesBehavior);
            }else {
                //4.2 存在，修改不喜欢记录
                unlikesBehavior.setType(dto.getType());
                unlikesBehavior.setCreatedTime(new Date());
                updateById(unlikesBehavior);
            }
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            throw new LeadNewsException(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    @Override
    public ResponseResult<ApUnlikesBehavior> getUnlikeBehavior(Integer entryId, Long articleId) {
        QueryWrapper<ApUnlikesBehavior> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("entry_id",entryId);
        queryWrapper.eq("article_id",articleId);
        return ResponseResult.okResult(getOne(queryWrapper));
    }
}
